package servlet.man;

import bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.UserDaoImpl;
import utils.JwtUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author 李夏
 * @version 1.0
 */
//管理员修改个人信息
@WebServlet("/man/changeManInfo")
public class ChangeManInfoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String manName = request.getParameter("manName");
        String password = request.getParameter("password");
        String user_token = request.getHeader("user_token_header");
        Integer manId = JwtUtils.getManIdFromToken(user_token,"manId");
        int age = 0;
        if (request.getParameter("age") != null) {
            age = Integer.parseInt(request.getParameter("age"));

        }
        int sex = 0;
        if (request.getParameter("sex") != null) {
            sex = Integer.parseInt(request.getParameter("sex"));

        }
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        Integer data= null;
        try {
            data = UserDaoImpl.changeStuInfoMan(manName, manId, age, sex, phone, email,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        Result result = new Result();
        if(data!=null){
            result.success(data);
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }else{
            result.fail();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }
    }
}
